﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using webapi.Entity;
using webapi.Models;

namespace webapi.Controllers
{
    public class SelectedController : ApiController
    {
      [HttpGet]
      public IHttpActionResult getAllStudentCourse(int curr, int limit)
      {
        List<Selected> courses = new List<Selected>();
        DataRowCollection dr = SelectedService.getAllStudentCourse();
        int j = curr - 1;
        int count = dr.Count;
        int len = count;
        if (count > limit)
        {
          int z = count - (j * limit);
          len = z >= limit ? limit : z;
        }
        for (int i = j * limit; i < len + j * limit; i++)
        {
          courses.Add(new Selected
          {
            course_name = dr[i][0].ToString().Trim(),
            course_id = dr[i][1].ToString().Trim(),
            class_name_id = dr[i][2].ToString().Trim(),
            point = (double)dr[i][3],
            is_must = (int)dr[i][4],
            student_name = dr[i][5].ToString().Trim(),
            student_id = dr[i][6].ToString().Trim(),
          });
        }
        return Json<dynamic>(new { code = 0, count, data = courses, });
      }

      [HttpGet]
      public IHttpActionResult getCourseByStudentId(string id)
      {
        List<Selected> courses = new List<Selected>();
        DataRowCollection dr = SelectedService.getStudentCourseById(id);
        int len = dr.Count;
        for (int i = 0; i < len; i++)
        {
          courses.Add(new Selected
          {
            course_name = dr[i][0].ToString().Trim(),
            course_id = dr[i][1].ToString().Trim(),
            class_name_id = dr[i][2].ToString().Trim(),
            point = (double)dr[i][3],
            is_must = (int)dr[i][4],
            student_name = dr[i][5].ToString().Trim(),
            student_id = dr[i][6].ToString().Trim(),
          });
        }
        return Json<dynamic>(new { code = 0, data = courses, });
      }
    
      [HttpGet]
      public IHttpActionResult getMyData()
      {
        List<Student> students = new List<Student>();
        List<Course> courses = new List<Course>();
        DataRowCollection dr = CourseService.getAllClass();
        DataRowCollection dr2 = UserService.getAllStudent();
        int len = dr.Count;
        int len2 = dr2.Count;
        for (int i = 0; i < len; i++)
        {
          courses.Add(new Course
          {
            course_id = dr[i][1].ToString().Trim(),
            name = dr[i][0].ToString().Trim(),
            class_course_id = dr[i][2].ToString().Trim(),
          });
        }
        for (int i = 0; i < len2; i++)
        {

          students.Add(new Student
          {
            user_id = (int)dr2[i][1],
            name = dr2[i][3].ToString().Trim()
          });
        }
        return Json<dynamic>(new { code = 0, students, courses, dr });
      }
    
    }
}
